package com.leetcode.studyplan.algorithm.basis;



/**
 * 输入：nums = [5,7,7,8,8,10], target = 8
 * 输出：[3,4]
 *
 * 输入：nums = [5,7,7,8,8,10], target = 6
 * 输出：[-1,-1]
 *
 * 输入：nums = [], target = 0
 * 输出：[-1,-1]
 * 二分查找很难写，用双指针方便很多，就是时间复杂度不能达到log(n)
 */
public class L_34_FindFirstAndLastPositionOfElementInSortedWay {

    public int[] searchRangeByTwoPoints(int[] nums, int target) {

        int[] res = new int[]{-1, -1};
        if (nums == null || nums.length == 0) {
            return res;
        }

        int leftPoint = 0;
        int rightPoint = nums.length - 1;

        while (leftPoint < nums.length && nums[leftPoint] != target) {
            leftPoint++;
        }

        if (leftPoint == nums.length) {
            return res;
        }
        res[0] = leftPoint;

        while (rightPoint > leftPoint && nums[rightPoint] != target) {
            rightPoint--;
        }

        if (nums[rightPoint] == target) {
            res[1] = rightPoint;
        }


        return res;
    }

    public static void main(String[] args) {
        int[] nums = {5,7,7,8,8,10};
        L_34_FindFirstAndLastPositionOfElementInSortedWay first = new L_34_FindFirstAndLastPositionOfElementInSortedWay();
        int[] res = first.searchRangeByTwoPoints(nums, 6);
        System.out.println(res);
    }

}
